<template>
  <div class="picture-resize-body">
    <div class="picture-resize-container">
      <div class="picture-resize-header">
        <h1><el-icon><Picture /></el-icon> Image Resize Tool</h1>
        <p>Upload and resize your images easily</p>
      </div>

      <el-form :model="form" label-width="120px" @submit.prevent="handleSubmit" label-position="left">
        <el-form-item label="Select Image" >
          <div
            class="upload-area"
            :class="{ 'drag-over': isDragOver }"
            @click="triggerFileInput"
            @dragenter.prevent="handleDragEnter"
            @dragover.prevent="handleDragOver"
            @dragleave.prevent="handleDragLeave"
            @drop.prevent="handleDrop"
          >
            <el-icon class="upload-icon"><Upload /></el-icon>
            <p>Click or drag image to this area to upload</p>
            <input
              ref="fileInput"
              type="file"
              accept="image/*"
              @change="handleFileChange"
              style="display: none;"
            >
          </div>
          <el-image
            v-if="previewUrl"
            :src="previewUrl"
            alt="Image Preview"
            class="preview-img"
            fit="contain"
          />
        </el-form-item>

        <el-form-item label="Scale Ratio">
          <el-input-number
            v-model="form.scale"
            :min="0.1"
            :max="1.0"
            :step="0.1"
            controls-position="right"
          />
          <span class="ratio-hint">Range: 0.1 - 1.0</span>
        </el-form-item>

        <el-form-item label="Format">
          <el-select v-model="form.formatter" style="width: 300px" placeholder="Select format">
            <el-option label="JPG" value="jpg" />
            <el-option label="PNG" value="png" />
            <el-option label="WebP" value="webp" />
            <el-option label="GIF" value="gif" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            native-type="submit"
            :disabled="!form.image"
            size="large"
          >
            <el-icon><Download /></el-icon> Resize & Download
          </el-button>
          <el-button
            @click="resetForm"
            size="large"
          >
            <el-icon><Refresh /></el-icon> Reset
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onUnmounted } from 'vue'
import { Picture, Upload, Download, Refresh } from '@element-plus/icons-vue'

// 响应式数据
const fileInput = ref(null)
const previewUrl = ref('')
const isDragOver = ref(false)

const form = reactive({
  image: null,
  scale: 0.5,
  formatter: 'jpg'
})

// 方法定义
const triggerFileInput = () => {
  fileInput.value.click()
}

const handleFileChange = (event) => {
  const file = event.target.files[0]
  if (file) {
    form.image = file
    showPreview(file)
  }
}

const handleDragEnter = () => {
  isDragOver.value = true
}

const handleDragOver = () => {
  isDragOver.value = true
}

const handleDragLeave = () => {
  isDragOver.value = false
}

const handleDrop = (event) => {
  isDragOver.value = false
  const file = event.dataTransfer.files[0]
  if (file && file.type.match('image.*')) {
    form.image = file
    showPreview(file)
    // 更新文件输入框的值
    if (fileInput.value) {
      const dataTransfer = new DataTransfer()
      dataTransfer.items.add(file)
      fileInput.value.files = dataTransfer.files
    }
  } else {
    ElMessage.warning('Please drop an image file!')
  }
}

const showPreview = (file) => {
  if (previewUrl.value) {
    URL.revokeObjectURL(previewUrl.value)
  }
  previewUrl.value = URL.createObjectURL(file)
}

const handleSubmit = () => {
  if (!form.image) {
    ElMessage.warning('Please select an image')
    return
  }

  // 构建FormData并提交
  const formData = new FormData()
  formData.append('image', form.image)
  formData.append('scale', form.scale)
  formData.append('formatter', form.formatter)

  // 这里应该发送请求到后端
  console.log('Submitting form:', Object.fromEntries(formData))
}

const resetForm = () => {
  form.image = null
  form.scale = 0.5
  form.formatter = 'jpg'

  if (previewUrl.value) {
    URL.revokeObjectURL(previewUrl.value)
    previewUrl.value = ''
  }

  if (fileInput.value) {
    fileInput.value.value = ''
  }
}

// 清理副作用
onUnmounted(() => {
  if (previewUrl.value) {
    URL.revokeObjectURL(previewUrl.value)
  }
})
</script>

<style scoped>
.picture-resize-body {
  padding: 50px;
}

.picture-resize-container {
  max-width: 660px;
  margin: 0 auto;
  background: white;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.picture-resize-header {
  text-align: center;
  margin-bottom: 30px;
}

.picture-resize-header h1 {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.upload-area {
  border: 2px dashed #e6e6e6;
  border-radius: 6px;
  padding: 30px;
  text-align: center;
  margin-bottom: 20px;
  transition: all 0.3s;
  cursor: pointer;
}

.upload-area:hover,
.upload-area.drag-over {
  border-color: #409eff;
  background-color: #f9f9f9;
}

.upload-icon {
  font-size: 48px;
  color: #c2c2c2;
}

.preview-img {
  max-width: 100%;
  max-height: 300px;
  display: block;
  margin: 15px auto;
  border-radius: 4px;
}

.ratio-hint {
  margin-left: 10px;
  color: #999;
}
</style>
